package com.mg.upload.controller;

import com.mg.common.vo.ResponseResult;
import com.mg.upload.entity.VideoUpload;
import com.mg.upload.service.VideoUploadService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/up")
public class UploadController {

    @Resource
    VideoUploadService videoUploadService;

    /**
     * 将传过来的文件保存
     * @param files 传输过来的文件
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile files) {
        int i=0;
        try {
            i = videoUploadService.uploadVideo(files);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (i==1){
            return ResponseResult.ok();
        }
        return ResponseResult.error();
    }

    /**
     * 获取传输文件的相关信息
     * @param videoUpload 传过来的文件的信息
     * @return
     */
    @PostMapping("/setVideo")
    public ResponseResult setVideo(VideoUpload videoUpload) {
        int i = videoUploadService.setVideo(videoUpload);
        if (i==1){
            return ResponseResult.ok();
        }
        return ResponseResult.error();
    }

    /**
     * 逻辑删除视频
     * @param id 通过id删除
     * @return
     */
    @DeleteMapping("/remove")
    public ResponseResult remove(int id){
        int remove = videoUploadService.remove(id);
        if (remove==1){
            return ResponseResult.ok();
        }
        return ResponseResult.error();
    }
}
